package model

import (
	"demo/handler/response"
	"gorm.io/gorm"
)

type Black struct {
	gorm.Model
	UserId   int `gorm:"type:int" json:"userId"`
	FriendId int `gorm:"type:int" json:"friendId"`
}

func (b *Black) IsBlack(db *gorm.DB, userId, friendId int) error {
	return db.Where("user_id = ? and friend_id = ?", userId, friendId).First(&b).Error
}

func (b *Black) AddBlack(db *gorm.DB) error {
	return db.Create(&b).Error
}
func (b *Black) GetBlack(db *gorm.DB, userId int) (list []response.FriendList, err error) {
	err = db.Model(&b).
		Select("users.phone,users.nick_name,users.avatar_url,users.refer,blacks.created_at").
		Joins("LEFT JOIN users ON users.id = blacks.friend_id").
		Where("blacks.user_id = ?", userId).Find(&list).Error
	return
}
